layui.use(['form','layer','table','laytpl','upload'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        upload = layui.upload,
        table = layui.table;
    var index = layer.load(1); //添加laoding,0-2两种方式
    
    
    //上传
    var uploadInst = upload.render({
      elem: '#upload' //绑定元素
      ,url: '../blackList/fileUpload/' //上传接口
      ,method: 'post'
      ,accept : "file"
	  ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
		  layer.load(); //上传loading
	  }
      ,done: function(res, index, upload){
    	layer.closeAll('loading'); //关闭loading
        //上传完毕回调
    	layer.msg('上传成功');
    	tableIns.reload();
      }
      ,error: function(){
        //请求异常回调
      }
    });
    
    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        method: 'post',
        url : '../blackList/dataGrid',
        cellMinWidth : 95,
        page : true,
        loading: true, //翻页加loading
        height : "full-135",
        limits : [10,15,20,25],
        limit : 20,
//        initSort: {
//            field: 'time' //排序字段，对应 cols 设定的各字段名
//            ,type: 'asc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
//        },
        id : "userListTable",
        cols : [[
        	{field: 'phone_no', title: '电话号码', minWidth:140, align:"center"},
            {field: 'cert_id', title: '身份证', minWidth:200, align:"center"},
            {field: 'imei_14', title: 'IMEI', minWidth:150, align:"center"},
            {field: 'county_name', title: '区县', minWidth:100, align:"center"},
            {field: 'user_state_name', title: '状态名称', minWidth:100, align:"center"},
            {field: 'onnet_day_num', title: '在网天数', minWidth:100, align:"center"},
            {field: 'zj', title: '主叫次数', minWidth:100, align:"center"},
            {field: 'bj', title: '被叫次数', minWidth:100, align:"center"},
            {field: 'my_zj', title: '漫游主叫', minWidth:100, align:"center"},
            {field: 'my_bj', title: '漫游被叫', minWidth:100, align:"center"},
            {field: 'zj_gs', title: '主叫号码个数', minWidth:100, align:"center"},
            {field: 'bj_gs', title: '被叫号码个数', minWidth:100, align:"center"},
            {field: 'type', title: '漫游占比', minWidth:100, align:"center"},
            {field: 'time', title: '判断时间', minWidth:100, align:"center"},
            {field: 'data_source', minWidth:100, title: '数据源', align:'center',templet:function(d){
            	return d.data_source == "0" ? "一线平台" : "用户导入";
            }},
            {field: 'send_message', minWidth:100, title: '是否发送短信', align:'center',templet:function(d){
            	return d.send_message == "0" ? "是" : "否";
            }},
            {field: 'is_answer', minWidth:100, title: '用户是否回复', align:'center',templet:function(d){
            	return d.is_answer == "0" ? "是" : "否";
            }},
            {field: 'answer_state', minWidth:100, title: '回复状态(内容)', align:'center',templet:function(d){
            	if(d.answer_state == "0"){
            		return "是";
            	}else if (d.answer_state == "1"){
            		return "否";
            	}else if (d.answer_state == "2"){
            		return "超时";
            	}else{
            		return "不详";
            	}
            }},
            {field: 'stat_date', title: '日期', minWidth:100, align:"center"},
        ]],
        done:function (res) {   //返回数据执行回调函数
	    	layer.close(index);    //返回数据关闭loading
	    }
    });

    //搜索
    $(".search_btn").on("click",function(){
    	var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
        if($(".searchVal").val().trim() != ''){
        	setTimeout(function(){
        		table.reload("userListTable",{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                    	phone_no: $(".searchVal").val().trim(),  //搜索的关键字
                    	send_message:  $("#send_message option:selected").val(),
                    	is_answer: $("#is_answer option:selected").val()
                    }
                });
    			layer.close(index);
    		},2000);
        }else{
        	setTimeout(function(){
        		table.reload("userListTable",{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                    	send_message:  $("#send_message option:selected").val(),
                    	is_answer: $("#is_answer option:selected").val()
                    }
                });
    			layer.close(index);
    		},2000);
        	layer.close(index);
        }
    }); 
    
    //导出
    $(".export_btn").click(function(){
//    	var cityName = $("#city").find("option:selected").text();
//		var industry = $("#industry").find("option:selected").text();
//		window.open("../test/down?cityName="+cityName+"&industry="+industry);
//		window.open("../test/downData");
    	window.open("../test/downBlackListData");
    });
    
    //数据模板
    $(".template_btn").click(function(){
		window.open("../test/downBlackListTmp");
    });
    
})
